var onResize = function () {
    $("body").css("padding-top", $(".navbar").outerHeight());
};

$(window).resize(onResize);

//显示回到顶部
$(window).scroll(function () {
    $('.to-top').hide();
    if ($(window).scrollTop() >= 300) {
        $('.to-top').show();
    }
    ;
});


$(function () {
    onResize();
    $("nav.navbar.navbar-expand-lg").autoHidingNavbar();

    //回到顶部
    $(".to-top").click(function () {
        var speed = 400; //滑动的速度
        $('body,html').animate({
            scrollTop: 0
        }, speed);
        return false;
    });
    //添加主页
    var queryString = getQueryString("model");
    if (queryString === 'register') {
        //添加注册页
        $(".index-content").load("/static/register.html");
    } else if (queryString === 'login') {
        //添加注册页
        $(".index-content").load("/static/login.html");
    } else if (queryString === 'forget') {
        //添加注册页
        $(".index-content").load("/static/forget-user.html");
    } else {
        $(".index-content").load("/static/home.html");
    }

    //用户信息
    var cookieJWT = getLocalStorage('malongxiaozhan-user-jwt', expUser);
    var cookieUserName = getLocalStorage('malongxiaozhan-user-userName', expUser);
    if (cookieJWT !== null && cookieUserName !== null && cookieUserName !== "null" && cookieJWT !== 'undefined' && cookieJWT !== undefined) {
        $(".user-login").show();
        $(".user-no-login").hide();
        $(".userNames").html(cookieUserName);
    }
});

function serarchArticle() {
    var searchValue = $(".article-serarch").val();
    //homeContentList(1, searchValue);
    var searchValues = encodeURIComponent(encodeURIComponent(searchValue));
    window.location.href = "/index.html?model=search&page=1&searchValue="+searchValues;
    $(".article-serarch").val("");
}

//获取url中的参数
function getQueryString(name) {
    var reg = new RegExp('(^|&)' + name + '=([^&]*)(&|$)', 'i');
    var r = window.location.search.substr(1).match(reg);
    if (r != null) {
        return unescape(r[2]);
    }
    return null;
}

function exitUser(){
    var token = getLocalStorage("malongxiaozhan-user-jwt", expUser);
    removeLocalStorage("malongxiaozhan-user-jwt");
    removeLocalStorage('malongxiaozhan-user-userName');
    $(".user-login").hide();
    $(".user-no-login").show();
    $(".userNames").html("");
    showHide();

    $.ajax({
        type: 'GET',
        url: '/blog/blogOperate/logOut',
        dataType: "json",
        async: false,
        headers: {
            "Content-Type": 'application/json',
            "token": token
        },//设置请求头
        success: function (result) {

        }
    });
}


//封装过期控制代码
function setLocalStorage(key,value){
    var curTime = new Date().getTime();
    localStorage.setItem(key,JSON.stringify({data:value,time:curTime}));
}

function getLocalStorage(key,exp){
    var data = localStorage.getItem(key);
    var dataObj = JSON.parse(data);
    if(dataObj ===null){
        return null
    }else if (new Date().getTime() - dataObj.time>exp) {
        return null;
    }else{
        return dataObj.data;
    }
}

function removeLocalStorage(key){
    localStorage.removeItem(key);
}

var expLogin = 1000*60*5;
var expUser = 1000*60*60*12;
